package com.example.coffeejava.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.coffeejava.Dto.PageResult;
import com.example.coffeejava.Dto.Result;
import com.example.coffeejava.Entity.Order;
import com.example.coffeejava.Entity.OrderItem;
import com.example.coffeejava.Entity.UserHolder;
import com.example.coffeejava.Mapper.OrderItemMapper;
import com.example.coffeejava.Mapper.OrderMapper;
import com.example.coffeejava.Service.OrderService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderItemMapper orderItemMapper;

    public Result getsjlist(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Order> page = orderMapper.getlistt(UserHolder.getUser().getId());
        PageResult pageResult = new PageResult();
        pageResult.setTotal((int) page.getTotal());
        pageResult.setRecords(page.getResult());
        return Result.ok(pageResult);

    }

    public Result fh(Long id) {
        Order order = orderMapper.selectById(id);
        order.setStatus("已发货");
        orderMapper.updateById(order);
        return Result.ok("成功");
    }
   public Result sh(Long id){
        Order order = orderMapper.selectById(id);
        order.setStatus("已收货");
        orderMapper.updateById(order);
        return Result.ok("成功");

    }
    public Result getlist(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Order> page = orderMapper.getlist(UserHolder.getUser().getId());
        for (Order order : page.getResult()) {

            List<OrderItem> list = orderItemMapper.selectbyorderId(order.getOrderId());

            order.setList(list);
        }
        PageResult pageResult = new PageResult();
        pageResult.setTotal((int) page.getTotal());
        pageResult.setRecords(page.getResult());
        return Result.ok(pageResult);

    }
}
